package org.ww.member.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;
import org.ww.common.exception.GmallExceptionEnum;
import org.ww.common.utils.PageUtils;
import org.ww.common.utils.R;
import org.ww.member.dto.Oauth2WeiboDto;
import org.ww.member.dto.UserLoginDto;
import org.ww.member.dto.UserRegistDto;
import org.ww.member.entity.MemberEntity;
import org.ww.member.exception.PhoneExistException;
import org.ww.member.exception.UserNameExistException;
import org.ww.member.feign.CouponFeignService;
import org.ww.member.service.MemberService;

import java.util.Arrays;
import java.util.Map;



/**
 * 会员
 *
 * @author ww
 * @email ww@qq.com
 * @date 2024-01-05 14:51:15
 */
@RestController
@RequestMapping("member/member")
public class MemberController {
    @Autowired
    private MemberService memberService;

    @Autowired
    private CouponFeignService couponFeignService;

    /**
     * oauth2的微博登录
     */
    @PostMapping("/oauth2/weibo/login")
    public R weiboLogin(@RequestBody Oauth2WeiboDto dto){
        MemberEntity entity = memberService.login(dto);
        if(ObjectUtils.isEmpty(entity)){
            return R.error(GmallExceptionEnum.USERNAME_PASSWORD_VALIDATED_EXCEPTION.getCode(),GmallExceptionEnum.USERNAME_PASSWORD_VALIDATED_EXCEPTION.getMsg());
        }else{
            return R.ok().setData(entity);
        }
    }

    /**
     * 登录
     */
    @PostMapping("/login")
    public R login(@RequestBody UserLoginDto dto){
        MemberEntity entity = memberService.login(dto);
        if(ObjectUtils.isEmpty(entity)){
            return R.error(GmallExceptionEnum.USERNAME_PASSWORD_VALIDATED_EXCEPTION.getCode(),GmallExceptionEnum.USERNAME_PASSWORD_VALIDATED_EXCEPTION.getMsg());
        }else{
            return R.ok().setData(entity);
        }
    }

    /**
     * 注册
     */
    @PostMapping("/register")
    public R register(@RequestBody UserRegistDto dto){
        try{
            memberService.register(dto);
        }catch (PhoneExistException phoneExistException){
            return R.error(GmallExceptionEnum.PHONE_EXIST_EXCEPTION.getCode(), GmallExceptionEnum.PHONE_EXIST_EXCEPTION.getMsg());
        }catch (UserNameExistException userNameExistException){
            return R.error(GmallExceptionEnum.USER_EXIST_EXCEPTION.getCode(), GmallExceptionEnum.USER_EXIST_EXCEPTION.getMsg());
        }
        return R.ok();
    }

    /**
     * 列表
     */
    @RequestMapping("/coupons")
    public R coupons(){
        MemberEntity memberEntity = new MemberEntity();
        memberEntity.setNickname("张三");
        R membercoupons = couponFeignService.membercoupons();

        // 打印会员和优惠券信息
        return R.ok().put("member",memberEntity).put("coupons",membercoupons.get("coupons"));

    }

    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = memberService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    public R info(@PathVariable("id") Long id){
		MemberEntity member = memberService.getById(id);

        return R.ok().put("member", member);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody MemberEntity member){
		memberService.save(member);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody MemberEntity member){
		memberService.updateById(member);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] ids){
		memberService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
